package com.sgb.member;

import android.app.Application;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.member.entity.CompanyDeptBean;
import com.sgb.member.entity.CompanyInfoEntity;
import com.sgb.member.entity.MemberCompanyBean;
import com.sgb.member.entity.MemberLoginEntity;
import com.sgb.member.entity.UserMsgEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;

import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainModel extends AndroidViewModel {

    public MutableLiveData<List<MemberCompanyBean>> companyList;
    public MutableLiveData<Boolean> mainCompany;

    public MainModel(@NonNull Application application) {
        super(application);
        companyList = new MutableLiveData<>();
        mainCompany = new MutableLiveData<>();
    }

    public void pwdLoginModel(String mobile, String pwd) {
        MemberConfig.getInstance().getApi().login(mobile, Base64.encodeToString(pwd.getBytes(), Base64.DEFAULT)).enqueue(new ICallBack<BaseEntity<MemberLoginEntity>>() {
            @Override
            public void onSuccess(BaseEntity<MemberLoginEntity> data) {
                MemberLoginEntity loginData = data.getData();
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, loginData.getToken());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourcePCToken, loginData.getResourcePCToken());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourceAPPToken, loginData.getResourceAPPToken());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PC_TOKEN, loginData.getResourcePCToken());
                getUserInfo(loginData);
            }
        });
    }

    public void getUserInfo(MemberLoginEntity loginData) {
        MemberConfig.getInstance().getApi().getUserInfo().enqueue(new ICallBack<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UserMsgEntity> data) {
                if (data.getData().getEnterprise() != null) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.SHOP_ID, data.getData().getEnterprise().getId());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ENTERPRISE_NO, data.getData().getEnterprise().getNo());
                }
                //给SharedPreferenceUtil中的USER_NAME赋值 保存用户姓名
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NAME, data.getData().getUserName());
                //给SharedPreferenceUtil中的USER_NO赋值 保存用户ID
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NO, data.getData().getNo());
                //给SharedPreferenceUtil中的REAL_NAME赋值 保存实名认证的状态
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_NAME, data.getData().getRealName());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ENTERPRISE_AUTH, data.getData().getEnterpriseAuth());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PERSON_AUTH, data.getData().isPersonAuth());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ID_CARD, data.getData().getIdCard());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.DRIVER_AUTH, data.getData().getIsDriverAuth());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COM_TYPE, data.getData().getUserType());
                //保存是否关联企业 0未关联  1已关联
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.RELEVANCE, data.getData().getRelevance());
                if (data.getData() != null && data.getData().getEnterprise() != null && !TextUtils.isEmpty(data.getData().getEnterprise().getNo())) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NAME, data.getData().getEnterprise().getCompanyName());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, data.getData().getEnterprise().getNo());
                    //给SharedPreferenceUtil中的REAL_ID赋值 保存登录人ID
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_ID, data.getData().getEnterprise().getId());
                }
                getCompanyList();
            }
        });

    }

    /**
     * 公司列表信息
     */
    public void getCompanyList() {
        MemberConfig.getInstance().getApi().getUserCompany().enqueue(new ICallBack<BaseEntity<List<MemberCompanyBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MemberCompanyBean>> data) {
                if (data != null && data.getData() != null) {
                    companyList.setValue(data.getData());
                }
            }
        });
    }

    /**
     * 设置主企业
     *
     * @param companyEntity
     */
    public void settingMainCompany(MemberCompanyBean companyEntity) {
        MemberConfig.getInstance().getApi().settingMainCompany(companyEntity.getNo(), Integer.parseInt(companyEntity.getType())).enqueue(new Callback<BaseEntity<MemberLoginEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MemberLoginEntity>> call, Response<BaseEntity<MemberLoginEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState() == null)return;
                if (response.body().getState().equals("ok")) {
                    // 保存 token
                    MemberLoginEntity entity = response.body().getData();
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourcePCToken, entity.getResourcePCToken());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourceAPPToken, entity.getResourceAPPToken());
                    //给SharedPreferenceUtil中的COMPANY_NO赋值 保存公司ID
                    Log.e("公司no", companyEntity.getNo());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, companyEntity.getNo());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ENTERPRISE_NO, companyEntity.getNo());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NAME, companyEntity.getName());
                    //查询个人中心商品列表
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_ID, companyEntity.getCompanyId());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COM_TYPE, Integer.parseInt(companyEntity.getType()));
                    mainCompany.setValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MemberLoginEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                mainCompany.setValue(false);
            }
        });
    }
}
